<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!isFollowed"
    @click="onFollowed"
    :loading="isFollowLoading"
    >关注</van-button
  >
  <van-button
    class="follow-btn"
    round
    size="small"
    @click="onFollowed"
    v-else
    :loading="isFollowLoading"
    >已关注</van-button
  >
</template>

<script>
import { followUser, deleteUser } from '@/api/user'

export default {
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    isAutId: [String, Number, Object]
  },
  data () {
    return {
      isFollowLoading: false
    }
  },
  model: {
    // 默认解析设置
    // prop: 'value',
    // event: 'input',
    // 指定解析设置
    prop: 'isFollowed',
    event: 'updateFollwoed'
  },
  created () {},

  methods: {
    async onFollowed () {
      try {
        this.isFollowLoading = true
        if (this.isFollowed) {
          await deleteUser(this.isAutId)
        } else {
          await followUser(this.isAutId)
        }
        this.$emit('updateFollwoed', !this.isFollowed)
        this.isFollowLoading = false
      } catch (err) {
        console.dir(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
